<?php 
@session_start();
require_once('../config/config.php');
require_once('../utils/CommonUtils.php');
require_once('../utils/DBUtils.php');
include ("../utils/Constants.php");
require_once('../utils/BaseDAO.php');
require("../utils/Session.php");

$userid = Session::get('userid');

$commonUtils = new CommonUtils();
$contentId = $commonUtils->getParameter("id", 0);
$baseDAO = new BaseDAO();
$content = $baseDAO->getItem("content", array("id", "image", "title", "brief", "content", "createdby", "createddate", "views"), $contentId);

$viewCount = $content["views"] + 1;
$values = array($contentId, $viewCount);
$baseDAO->update("content", array("id", "views"), $values);

$relateItems = $baseDAO->getItems("content", array("id", "title", "createddate"), "status = 1 and id <> " . $contentId, "order by createddate desc", 0, 6);
$authors = $baseDAO->getItems("content", array("id", "title", "createddate"), "status = 1 and createdby = " . $content['createdby_id'], "order by createddate desc", 0, 5);
$mostviews = $baseDAO->getItems("content", array("id", "title", "createddate", "views"), "status = 1", "order by views desc", 0, 5);
$comments = $baseDAO->getItems("comment", array("id", "content", "createddate", "createdby"), "status = 1 and contentid = " . $contentId, "order by createddate desc");
$author = $baseDAO->getItem("user", array("fullname", "email", "avatar"), $content['createdby_id']);
?>
<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<LINK REL="SHORTCUT ICON" HREF="../img/fm.ico"> 
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<title><?php echo $content['title'];?></title>
		<link rel="stylesheet" type="text/css" href="../css/style.css"/>
		<link rel="stylesheet" type="text/css" href="../css/jquery.alert.css"/>
		<link rel="stylesheet" type="text/css" href="../css/jquery.fancybox.css"/>
		<link href="../css/bootstrap.css" rel="stylesheet">
		
		<script type="text/javascript" src="../js/jquery-1.7.2.min.js"></script>
		<script type="text/javascript" src="../js/jquery.hind.js"></script>
		<script type="text/javascript" src="../js/jquery.fancybox.js"></script>
		<script type="text/javascript" src="../js/jquery.corner.js"></script>
		<script src="../js/jquery.alerts.js"></script>
		<script src="../js/bootstrap.min.js"></script>
	</head>
	<body>
		<div id="content">
			<?php include '../include/webheader.php';?>
			<div id="floating-box"></div>
			<div id="page">
				<div class="twocol_col1">
					<div id="floating-box_" class="createddate">
						<?php echo $commonUtils->formatDate($content['createddate'], 'd - M');?>
						<?php echo $commonUtils->formatDate($content['createddate'], 'Y');?>
					</div>
					<div class="detailTitle"><?php echo $content['title'];?></div>
					<?php if($content['brief'] != ""){?>
						<div class="detailBrief"><b><?php echo $content['brief'];?></b></div>
					<?php }?>
					<div class="detailContent"><?php echo $content['content'];?></div>
					<div class="title" style="float:left;"><div class="fb-like" data-send="false" data-layout="button_count" data-width="450" data-show-faces="false"></div></div>
					<div class="createdby"><?php echo $content['createdby'];?></div>
					<div style="clear:both"></div>
					<div class="fb-comments" data-width="655" href="http://www.nguyendau.com/bai-viet/chi-tiet.html?id=<?php echo $contentId;?>" data-num-posts="10"></div>
					
					<?php /*
					<div class="comments">
						<div id="txtInput">
							<input type="text" id="txtComment" name="content" title="Viết bình luận..." width="400px"/>
						</div>
						<div id="tbComments">
							<?php foreach($comments as $comment){?>
							<div class="comment">
								<span id="author"><?php echo $comment['createdby'] == null ? 'Không tên' : $comment['createdby'];?></span>
								<span id="comment"><?php echo $comment['content'];?></span>
								<?php if(isset($userid) && $comment['createdby_id'] == $userid){?>
									<span id="del"><a onclick="delcomment(<?php echo $comment['id'];?>);"><img src="../css/images/delete.png"/></a></span>
								<?php }?>
								<span id="date" class="displayDate"><?php echo $commonUtils->timePost($comment['createddate']);?></span>
							</div>
							<div style="clear:both;"></div>
							<?php }?>
						</div>
						
						<script type="text/javascript">
						$(function(){
							$('.myCorner').corner();
							$("#txtComment").keypress(function(e){
								if (e.which == 13){
									var cm = $("#txtComment").val();
									if(cm != null && cm != ""){
										if('<?php echo Session::get('fullname');?>' == ''){
											if(confirm('Vì bạn chưa đăng nhập nên bình luận sẻ được tác giả phê duyệt trước khi công khai!')){
												$.fancybox.showLoading();
												$.ajax({
													url: '../ajax/comment.php?id='+<?php echo $_GET['id'];?>+'&cm=' + cm,
													success:function(data){
														$("#txtComment").val("");
														alert("Bình luận của bạn đã gửi thành công, bình luận sẻ được hiển thị khi được tác giả phê duyệt");
														$.fancybox.hideLoading();
													}
												});	
											}
										}else{
											$.fancybox.showLoading();
											$.ajax({
												url: '../ajax/comment.php?id='+<?php echo $_GET['id'];?>+'&cm=' + cm,
												success:function(data){
													$("#txtComment").val("");
													loadComments();
													$.fancybox.hideLoading();
												}
											});	
										}
									}
								}
							});

							$("#txtComment").hint();

							$(".comment").hover(function(){
								$(this).addClass("comment_");
							}, function(){
								$(this).removeClass("comment_");
							});
							
						});
						function loadComments(){
							$.ajax({
								url: '../ajax/loadcomment.php?id='+<?php echo $_GET['id'];?>,
								dataType: 'html',
								success:function(html){
									$("#tbComments").html(html);

									$(".comment").hover(function(){
										$(this).addClass("comment_");
									}, function(){
										$(this).removeClass("comment_");
									});
								}
							});	
						}
						function delcomment(commentid){
							if(commentid != null && commentid > 0){
								if(confirm('Bạn có chắc chắn khi xóa bình luận này?')){
									$.fancybox.showLoading();
									$.ajax({
										url: '../ajax/delcomment.php?id='+commentid,
										success:function(data){
											loadComments();
											$.fancybox.hideLoading();
										}
									});	
								}
							}
						}
						</script>
					</div>
					<?php */?>
				</div>
				<div class="twocol_col2">
					<div data-spy="affix" data-offset-top="10" style="border-left: 1px solid #CCCCCC;width:266px;">
						<div class="relateItems">
							<p style="font-weight:bold">Tác giả:</p>
							<?php if($author['avatar'] != null && $author['avatar'] != ""){?>
								<p><img src="../images/avatar/<?php echo $author['avatar'];?>"/></p>
							<?php }?>
							<p><?php echo $author['fullname']; ?></p>
							<p><?php echo $author['email']; ?></p>
						</div>
						<div class="relateItems">
							<p style="font-weight:bold">Cùng tác giả:</p>
							<?php foreach($authors as $item){?>
								<div><a href="./chi-tiet.php?id=<?php echo $item["id"];?>"><?php echo $item["title"];?></a>  <span><?php echo $commonUtils->formatDate($item["createddate"], "d/m/Y")?></span></div>
							<?php }?>
						</div>
						
						<div class="relateItems">
							<p style="font-weight:bold">Xem nhiều:</p>
							<?php foreach($mostviews as $item){?>
								<div>[<?php echo ($item['views'] > 9 ? $item['views']: ' '.$item['views']);?>]&nbsp;<a href="./chi-tiet.php?id=<?php echo $item["id"];?>"><?php echo $item["title"];?></a>&nbsp;<span><?php echo $commonUtils->formatDate($item["createddate"], "d/m/Y")?></span></div>
							<?php }?>
						</div>
						<div style="clear:both"></div>
					</div>
				</div>
				<div style="clear:both"></div>
				<div class="relateItems">
					<p style="font-weight:bold">Xem thêm các bài viết mới nhất:</p>
					<?php foreach($relateItems as $item){?>
						<div><a href="./chi-tiet.php?id=<?php echo $item["id"];?>"><?php echo $item["title"];?></a>  <span><?php echo $commonUtils->formatDate($item["createddate"], "d/m/Y")?></span></div>
					<?php }?>
				</div>
			</div>
			<?php include '../include/webfooter.php';?>
		</div>
	</body>
</html>
